<template>
  <div>
    <div
      :id="id"
      :style="{
        width,
        height
      }"
    ></div>
  </div>
</template>
<script>
import * as echarts from "echarts";
export default {
  props: {
    width: {
      type: String,
      default: "400px"
    },
    height: {
      type: String,
      default: "400px"
    },
    id: {
      type: String,
      default: ""
    },
    originData: {
      type: Array,
      default: () => []
    },
    contrastData: {
      type: Array,
      default: () => []
    },
    axisLabel: {
      type: Array,
      default: () => []
    }
  },

  watch: {
    contrastData() {
      this.$nextTick(() => {
        this.init();
      });
    }
  },
  methods: {
    init() {
      const { originData, contrastData, axisLabel } = this.$props;
      const option = {
        tooltip: {
          trigger: "axis", //触发类型 柱状图
          axisPointer: { type: "shadow" }, //触发效果 移动上去 背景效果
          formatter: function (params) {
            var tooltipContent = params[0].name + "<br>";
            params.forEach(function (param) {
              if (param.seriesIndex !== 2) {
                tooltipContent += param.marker + " " + param.seriesName + ": " + param.value + "<br>";
              }
            });

            return tooltipContent;
          }
        },
        legend: {
          show: true,
          itemWidth: 8,
          itemHeight: 8,
          right: "25",
          top: "25",
          textStyle: {
            fontSize: 12,
            lineHeight: 12,
            color: "#979797",
            rich: {
              a: {
                verticalAlign: "middle"
              }
            },
            padding: [0, 0, -2, 0] //[上、右、下、左]
          }
        },
        xAxis: [
          {
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#99B3C8"
              }
            },
            axisLabel: {
              color: "#FFFFFF",
              fontSize: 12
            },
            type: "category",
            data: axisLabel
          },
          //背景柱体，不显示数据在图表中，只显示背景色
          {
            type: "category",
            show: false,
            data: ["讲授时长", "互动时长", "指导时长", "板书时长"]
          }
        ],
        yAxis: {
          name: "时长/分钟",
          nameTextStyle: {
            color: "#99B3C8"
          },
          type: "value",
          min: 0,
          axisLine: {
            show: false
          },
          axisLabel: {
            color: "#99B3C8",
            fontSize: 12,
            formatter: "{value} 分钟"
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: "dashed",
              color: "#99B3C8"
            }
          },
          axisTick: {
            show: false
          }
        },
        grid: {
          top: 50,
          left: 10,
          right: 40,
          bottom: 10,
          containLabel: true
        },
        series: [
          {
            name: "课堂一",
            type: "bar",
            barWidth: 20,
            barGap: 0.2,
            data: originData,
            itemStyle: {
              borderRadius: 2,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(98, 218, 217, 1)" },
                { offset: 1, color: "rgba(97, 215, 215, 0.01)" }
              ])
            }
          },
          {
            name: "课堂二",
            type: "bar",
            barWidth: 20,
            data: contrastData,
            itemStyle: {
              borderRadius: 2,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(255, 162, 33, 1)" },
                { offset: 1, color: "rgba(236, 236, 236, 0.01)" }
              ])
            }
          },
          {
            type: "bar",
            barWidth: 40,
            // barGap:10,
            xAxisIndex: 1,
            data: [0, 0, 0, 0, 0],
            showBackground: true
          }
        ]
      };
      const dom = document.getElementById(this.$props.id);
      const lineCharts = echarts.init(dom);
      lineCharts.setOption(option);
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.init();
    });
  }
};
</script>
